<?php

namespace App\Admin\Controllers\Agents;

use App\Admin\Repositories\AgentPrice;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AgentPriceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AgentPrice(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('agent_id')->filter();
            $grid->column('min_price')->sortable();
            $grid->column('max_price')->sortable();
            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();
        
            // $grid->disableCreateButton();
            
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('agent_id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AgentPrice(), function (Show $show) {
            $show->field('id');
            $show->field('agent_id');
            $show->field('min_price');
            $show->field('max_price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AgentPrice(), function (Form $form) {
            $form->display('id');
            $form->text('agent_id');
            $form->text('min_price');
            $form->text('max_price');
        
            $form->display('created_at');
            $form->display('updated_at');

            //编辑缓存同步
            $form->saved(function(Form $form){
                cache()->forget("video_price_conf:agent_".$form->model()->agent_id);
            });

            $form->deleted(function (Form $form, $result) {
                // 获取待删除行数据，这里获取的是一个二维数组
                $data = $form->model()->toArray();
                foreach($data as $item){
                    cache()->forget("video_price_conf:agent_".$item['agent_id']);
                }
            });

        });
    }
}
